/**
 * The javascript file for the hospital view
 */

/**
 * The id of the input element in the DOM
 * @private
 * @const
 */
var datePickerId = "#slotPicker";

/**
 * The id of the start time input
 * @private
 * @const
 */
var startTimeId = "#startSelector";

/**
 * The id of the start time input
 * @private
 * @const
 */
var endTimeId = "#endSelector";

HEALTH.setRole(HEALTH_ROLE_HOSPITAL);
$(document).ready(function() {
	HEALTH.initPage("dataGrid", ["day","start","end","type","doctor","patient",function (dataSource, type, param) {
		if (dataSource["free"] == true) {
			return "<button class='btn' onclick='deleteSlot(" + dataSource["id"] + ",false);'>Slot l&ouml;schen</button>";
		} else {
			return "<button class='btn btn-danger' onclick='deleteSlot(" + dataSource["id"] + ",true);'>Slot löschen</button>";
		}

	}]);

	$(".datepicker").datetimepicker({
		"pickTime": false
	});
	$(".timepicker").datetimepicker({
		"pickDate":false,
		"pickTime": true,
		"format": 'HH:mm',
		"showSeconds": false
	});
});

//export required public functions
HEALTH.exportSymbol("createSlot", createSlot);
HEALTH.exportSymbol("deleteSlot", deleteSlot);

/**
 * Function for creating a new surgery slot. The function uses the values of the elements in the modal dialog
 */
function createSlot() {
	/**
	 * @const
	 */
	var messageTitle = "OP Slot kann nicht erstellt werden";

	/**
	 * @const
	 */
	var messagePanel = "validationMessages";

	//first, remove all old validation messages
	$("#" + messagePanel).empty();

	//retrieve the values of the controls
	var dateInput = $(datePickerId).val();
	var startTime = $(startTimeId).val();
	var endTime = $(endTimeId).val();

	//checks if the input isn't empty
	if (dateInput.trim().length == 0) {
		HEALTH.showNotification(messagePanel, "error", messageTitle, "Das Datum darf nicht leer sein");
		return;
	}
	if (startTime.trim().length == 0) {
		HEALTH.showNotification(messagePanel, "error", messageTitle, "Anfangszeit darf nicht leer sein");
		return;
	}
	if (endTime.trim().length == 0) {
		HEALTH.showNotification(messagePanel, "error", messageTitle, "Endzeit darf nicht leer sein");
		return;
	}
	//check the format
	var dateParts = dateInput.split(".");
	if (dateParts.length != 3) {
		//this shouldn't happen acutally	
		HEALTH.showNotification(messagePanel, "error", messageTitle, "Falsches Datumsformat. Erwartetes Format: DD.MM.YYYY");
		return;
	}
	var startParts = startTime.split(":");
	var endParts = endTime.split(":");
	if (startParts.length != 2 || endParts.length != 2) {
		HEALTH.showNotification(messagePanel, "error", messageTitle, "Falsches Zeitformat. Erwartetes Format: HH:MM");
		return;
	}
	//create date objects
	var startDate = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0], startParts[0], startParts[1], 0, 0);
	var endDate = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0], endParts[0], endParts[1], 0, 0);
	if (startDate > endDate) {
		HEALTH.showNotification(messagePanel, "error", messageTitle, "Endzeit darf nicht vor der Startzeit liegen");
		return;
	}

	//okay, everything is fine
	HEALTH.callSetter("hospital/createSlot", {"startDate":startDate.getTime(),"endDate":endDate.getTime()});

	//add success message
	HEALTH.showNotification(messagePanel, "success", "OP Slot gespeichert", "Der OP Slot wurde neu erstellt");
	//delete all messages after a while
	setTimeout(function() {
		$("#" + messagePanel).empty()
	}, 3000);
	//reset the controls
	$(datePickerId).val("");
	$(startTimeId).val("");
	$(endTimeId).val("");
	//reload the data table
	HEALTH.reloadMainTable();
}

/**
 * Deletes the given slot
 * @param slotId
 * @param confirmation
 */
function deleteSlot(slotId, confirmation) {
	var performDelete = !confirmation;
	if (confirmation) {
		performDelete = confirm("Wollen Sie diesen Slot wirklich löschen und die reservierte Operation stornieren?");
	}
	if (performDelete) {
		HEALTH.callSetter("hospital/deleteSlot", {"slotId":slotId});
		HEALTH.reloadMainTable();
	}
}
